home *** CD-ROM | disk | FTP | other *** search
/ Mac OS 9 Serial Number Archive / SN Archive 2023.11.04.toast / BSNG / SDK / BSNG SDK 1.0.2 / Example Plugin / Example.c next >
Encoding:
C/C++ Source or Header  |  1997-10-08  |  9.5 KB  |  403 lines  |  [TEXT/CWIE]

  1. /*
  2.  * $Workfile:: Example.c                                                      $
  3.  * $Revision:: 3                                                              $
  4.  *
  5.  * $Author:: Buck Rogers                                                      $
  6.  * $Modtime:: 08.10.1997 02:03 Uhr                                            $
  7.  *
  8.  * $History:: Example.c                                                       $
  9.  * 
  10.  * *****************  Version 3  *****************
  11.  * User: Buck Rogers  Date: 08.10.1997   Time: 02:03 Uhr
  12.  * Updated in $/BSNG/Plugins/BSNG SDK/Example Plugin
  13.  * changed lf to cr in list generation so Mac compatible output is created
  14.  * 
  15.  * *****************  Version 2  *****************
  16.  * User: Buck Rogers  Date: 05.10.1997   Time: 19:24 Uhr
  17.  * Updated in $/BSNG/Plugins/BSNG SDK/Example Plugin
  18.  * Added comments related to the new name, company and numCopies items in the
  19.  * parameter block
  20.  * 
  21.  * *****************  Version 1  *****************
  22.  * User: Buck Rogers  Date: 30.09.1997   Time: 18:31 Uhr
  23.  * Created in $/BSNG/Plugins/BSNG SDK/Example Plugin
  24.  * Adding subproject 'BSNG' to '$/'
  25.  *
  26.  * $NoKeywords::                                                              $
  27.  */
  28.  
  29.  
  30. #include <MixedMode.h>
  31. #include <A4Stuff.h>
  32.  
  33. #include "standard utils.h"
  34. #include "UltraU.h"
  35. #include "BSNG API.h"
  36.  
  37.  
  38. /*    don't touch this __procinfo definition or the calls from native code will crash the machine */
  39.  
  40. ProcInfoType __procinfo = kThinkCStackBased | STACK_ROUTINE_PARAMETER(1, SIZE_CODE(sizeof(BSNGParamBlockPtr)));
  41.  
  42.  
  43. /*    function prototypes for ANSI C */
  44.  
  45. static void DoInit(BSNGParamBlockPtr inData);
  46. static Boolean DoValidate(BSNGParamBlockPtr inData);
  47. static void DoCalc(BSNGParamBlockPtr inData);
  48. static void DoRandomCalc(BSNGParamBlockPtr inData);
  49. static Boolean DoAddRandomsToList(BSNGParamBlockPtr inData);
  50. static void DoItemHit(BSNGParamBlockPtr inData);
  51. static void DoCleanup(BSNGParamBlockPtr inData);
  52. void main(BSNGParamBlockPtr inData);
  53.  
  54.  
  55. /*    here is where the fun starts :-) */
  56.  
  57. static void DoInit(BSNGParamBlockPtr inData)
  58. {
  59.     StringPtr        aStr = nil;
  60.     
  61.     
  62.     /*    Initialize the Ultra Random Number generator (optional, use MacOS random functions otherwise) */
  63.     
  64.     Ultra_seed1 = inData->randSeed1;
  65.     Ultra_seed2 = inData->randSeed2;
  66.     Ultra_Init();
  67.     
  68.     /*    Give the BSNG App the informations if your plugin can do random calculations and if it can write
  69.         to the serial number list */
  70.     
  71.     inData->wantsRandomButton = true;
  72.     inData->canAddToSNList = true;
  73.     
  74.     /*    Initialize the EditFields with initial default values (optional, they are empty otherwise) */
  75.     /*    if you create a name-based generator please make sure that you copy the name, company and/or numCopies */
  76.     /*    values into the corrosponding fields */
  77.     
  78.     aStr = inData->itemValue[kItemValue1];
  79.     BlockMoveData("\p12345678", aStr, 9);
  80.     
  81.     aStr = inData->itemValue[kItemValue2];
  82.     BlockMoveData("\p23456789", aStr, 9);
  83.     
  84.     aStr = inData->itemValue[kItemValue3];
  85.     BlockMoveData("\p34567890", aStr, 9);
  86.     
  87.     aStr = inData->itemValue[kItemValue4];
  88.     aStr[0] = 0;
  89.     
  90.     /*    Tell the BSNG App if the initialisation was successful (it was in our case) */
  91.     
  92.     inData->error = errExtNoErr;
  93.     inData->errorInItem = 0;
  94. }  /* DoInit */
  95.  
  96.  
  97. static Boolean DoValidate(BSNGParamBlockPtr inData)
  98. {
  99.     StringPtr        num1Str = inData->itemValue[kItemValue1];
  100.     StringPtr        num2Str = inData->itemValue[kItemValue2];
  101.     StringPtr        num3Str = inData->itemValue[kItemValue3];
  102.     Boolean            error = false;
  103.     
  104.     
  105.     /*    we do some simple checks here if the edit fields are not empty or not longer than 8 characters, additionally we could
  106.         also check if the values are really only numbers, but I skipped that because the input filter for the edit fileds
  107.         was set to integer anyway (defined in Constructor) */
  108.     
  109.     if ((num1Str[0] < 1) || (num1Str[0] > 8))
  110.     {
  111.         /*    if something was wrong in the input tell the BSNG App what edit field contained wrong values */
  112.         
  113.         inData->errorInItem = kEditItem1;
  114.         error = true;
  115.     }
  116.     else if ((num2Str[0] < 1) || (num2Str[0] > 8))
  117.     {
  118.         inData->errorInItem = kEditItem2;
  119.         error = true;
  120.     }
  121.     else if ((num3Str[0] < 1) || (num3Str[0] > 8))
  122.     {
  123.         inData->errorInItem = kEditItem3;
  124.         error = true;
  125.     }
  126.     
  127.     if (error)
  128.     {
  129.         inData->error = errExtIncorrectValue;
  130.         return (false);
  131.     }
  132.     
  133.     inData->error = errExtNoErr;
  134.     
  135.     return (true);
  136. }  /* DoValidate */
  137.  
  138.  
  139. static void DoCalc(BSNGParamBlockPtr inData)
  140. {
  141.     StringPtr        num1Str = inData->itemValue[kItemValue1];
  142.     StringPtr        num2Str = inData->itemValue[kItemValue2];
  143.     StringPtr        num3Str = inData->itemValue[kItemValue3];
  144.     StringPtr        addResultStr = inData->itemValue[kItemValue4];
  145.     
  146.     unsigned long    result = 0L;
  147.     long            value = 0L;
  148.     Boolean            error = false;
  149.     short            i = 0;
  150.     
  151.     
  152.     /*    very simply algorithm, just add the 3 numbers and set the result */
  153.     
  154.     StringToNum(num1Str, &value);
  155.     result += value;
  156.     
  157.     StringToNum(num2Str, &value);
  158.     result += value;
  159.     
  160.     StringToNum(num3Str, &value);
  161.     result += value;
  162.     
  163.     NumToString(result, addResultStr);
  164.     
  165.     /*    put the result on the Clipboard so users can copy and paste the serial number into the app they want to register */
  166.     
  167.     addResultStr[addResultStr[0] + 1] = '\0';
  168.     
  169.     ZeroScrap();
  170.     PutScrap(addResultStr[0], 'TEXT', &(addResultStr[1]));
  171. }  /* DoCalc */
  172.  
  173.  
  174. static void DoItemHit(BSNGParamBlockPtr inData)
  175. {
  176.     switch (inData->itemMessage)
  177.     {
  178.         case (300000):
  179.         
  180.             /*    the "About this plugin" button was pressed, do whatever you want, I just display a small Alert here */
  181.         
  182.             NoteAlert(1000, nil);
  183.             break;
  184.         
  185.         default:
  186.             break;
  187.     }
  188. }  /* DoItemHit */
  189.  
  190.  
  191. static void DoRandomCalc(BSNGParamBlockPtr inData)
  192. {
  193.     StringPtr        num1Str = inData->itemValue[kItemValue1];
  194.     StringPtr        num2Str = inData->itemValue[kItemValue2];
  195.     StringPtr        num3Str = inData->itemValue[kItemValue3];
  196.     
  197.     
  198.     /*    create your own random numbers as the random functions are not dependent on user input */
  199.     
  200.     num1Str[0] = 0;                                    /*    empty the string */
  201.     myBaseBRandomPStr(num1Str, 10, 8);                /*    generate 8 random digits */
  202.     
  203.     num2Str[0] = 0;
  204.     myBaseBRandomPStr(num2Str, 10, 8);
  205.     
  206.     num3Str[0] = 0;
  207.     myBaseBRandomPStr(num3Str, 10, 8);
  208.     
  209.     /*    now we are prepared and can call our Calc algorithm */
  210.     
  211.     DoCalc(inData);
  212. }  /* DoRandomCalc */
  213.  
  214.  
  215. static Boolean DoAddRandomsToList(BSNGParamBlockPtr inData)
  216. {
  217.     StringPtr        num1Str = inData->itemValue[kItemValue1];
  218.     StringPtr        num2Str = inData->itemValue[kItemValue2];
  219.     StringPtr        num3Str = inData->itemValue[kItemValue3];
  220.     StringPtr        addResultStr = inData->itemValue[kItemValue4];
  221.     
  222.     long            count = 0L;
  223.     short            i = 0;
  224.     OSErr            err = noErr;
  225.     
  226.     
  227.     /*    write your header to the number list */
  228.     
  229.     count = 24L;
  230.     err = FSWrite(inData->outputRefNum, &count, "BSNG SDK Example Plugin\r");
  231.     
  232.     /*    return false if an error occured, the creation of list will then be completly aborted */
  233.     
  234.     if (err != noErr)
  235.     {
  236.         return (false);
  237.     }
  238.     
  239.     count = 24L;
  240.     err = FSWrite(inData->outputRefNum, &count, "=======================\r");
  241.     
  242.     if (err != noErr)
  243.     {
  244.         return (false);
  245.     }
  246.     
  247.     /*    create numOfListNumbers random serial numbers and write them to the list */
  248.     /*    if you create a name-based generator please make sure that you create at least one number */
  249.     /*    using the name, company and/or numCopies informations. You could create more name-based numbers */
  250.     /*    by using your own name/company database */
  251.     
  252.     for (i = 0; i < inData->numOfListNumbers; i++)
  253.     {
  254.         DoRandomCalc(inData);
  255.         
  256.         count = (long) num1Str[0];
  257.         err = FSWrite(inData->outputRefNum, &count, &(num1Str[1]));
  258.         
  259.         if (err != noErr)
  260.         {
  261.             return (false);
  262.         }
  263.     
  264.         count = 3L;
  265.         err = FSWrite(inData->outputRefNum, &count, " + ");
  266.         
  267.         if (err != noErr)
  268.         {
  269.             return (false);
  270.         }
  271.         
  272.         count = (long) num2Str[0];
  273.         err = FSWrite(inData->outputRefNum, &count, &(num2Str[1]));
  274.         
  275.         if (err != noErr)
  276.         {
  277.             return (false);
  278.         }
  279.     
  280.         count = 3L;
  281.         err = FSWrite(inData->outputRefNum, &count, " + ");
  282.         
  283.         if (err != noErr)
  284.         {
  285.             return (false);
  286.         }
  287.         
  288.         count = (long) num3Str[0];
  289.         err = FSWrite(inData->outputRefNum, &count, &(num3Str[1]));
  290.         
  291.         if (err != noErr)
  292.         {
  293.             return (false);
  294.         }
  295.     
  296.         count = 3L;
  297.         err = FSWrite(inData->outputRefNum, &count, " = ");
  298.         
  299.         if (err != noErr)
  300.         {
  301.             return (false);
  302.         }
  303.         
  304.         count = (long) addResultStr[0];
  305.         err = FSWrite(inData->outputRefNum, &count, &(addResultStr[1]));
  306.         
  307.         if (err != noErr)
  308.         {
  309.             return (false);
  310.         }
  311.     
  312.         count = 1L;
  313.         err = FSWrite(inData->outputRefNum, &count, "\r");
  314.         
  315.         if (err != noErr)
  316.         {
  317.             return (false);
  318.         }
  319.     }
  320.     
  321.     /* don't forget this last newline */
  322.     
  323.     count = 1L;
  324.     err = FSWrite(inData->outputRefNum, &count, "\r");
  325.     
  326.     if (err != noErr)
  327.     {
  328.         return (false);
  329.     }
  330.     
  331.     return (true);
  332. }  /* DoAddRandomsToList */
  333.  
  334.  
  335. static void DoCleanup(BSNGParamBlockPtr inData)
  336. {
  337. #pragma unused (inData)
  338.  
  339.     /*    we didn't allocate any memory in DoInit, so we can leave this empty, otherwise it would be a VERY good idea
  340.         to deallocate/dispose etc. everything we allocated during our work. If you don't do that we have a nice memory leak */
  341.     
  342. }  /* DoCleanup */
  343.  
  344.  
  345. void main(BSNGParamBlockPtr inData)
  346. {
  347. #if (!GENERATINGPOWERPC)
  348.     EnterCodeResource();
  349. #endif
  350.     
  351.     /*    the message dispatcher */
  352.     
  353.     switch(inData->theMessage)
  354.     {
  355.         case (msgExtInit):
  356.             DoInit(inData);
  357.             break;
  358.         
  359.         case (msgExtCalcHit):
  360.         
  361.             if (DoValidate(inData))
  362.             {
  363.                 DoCalc(inData);
  364.             }
  365.             
  366.             break;
  367.         
  368.         case (msgExtRandomHit):
  369.             DoRandomCalc(inData);
  370.             break;
  371.         
  372.         case (msgExtCreateRandom):
  373.             
  374.             /*    report to the BSNG App if your list entry was written ok or if we had an error */
  375.             
  376.             if (DoAddRandomsToList(inData))
  377.             {
  378.                 inData->error = errExtNoErr;
  379.             }
  380.             else
  381.             {
  382.                 inData->error = errExtWritingToList;
  383.             }
  384.             
  385.             break;
  386.         
  387.         case (msgExtItemHit):
  388.             DoItemHit(inData);
  389.             break;
  390.         
  391.         case (msgExtCleanup):
  392.             DoCleanup(inData);
  393.             break;
  394.         
  395.         default:
  396.             break;
  397.     }
  398.     
  399. #if (!GENERATINGPOWERPC)
  400.     ExitCodeResource();
  401. #endif
  402. }  /* main */
  403.